import { createMemoryHistory, createRouter, createWebHashHistory } from 'vue-router'

import Layout from '../components/layout/index.vue'
import { ElMessage } from 'element-plus'

const routes = [
    // 首页
    { 
      path: '/',
      meta: {
        title: '首页'
      },
      name: 'index',
      redirect: '/login',
    },

    // 用户登录
    { 
      path: '/login',
      meta: {
        title: '用户登录'
      },
      name: 'login',
      component: () => import('@/views/login/index.vue')
    },

    // 仪表盘
    {
      path: '/dashboard',
      name: 'dashboard',
      meta: {
        title: '仪表盘',
        icon: 'Odometer',
        isMenu: true,
        breadcrumb: true,
        auth: true,
      },
      component: Layout,
      redirect: '/dashboard',
      children: [
        {
          path: '',
          meta: {
            title: '仪表盘',
            hidden: true,
            auth: true,
          },
          component: () => import('@/views/dashboard/index.vue')
        }
      ]
    },

    // 系统管理
    {
      path: '/system',
      name: 'system',
      meta: {
        title: '系统管理',
        icon: 'Setting',
        isMenu: false,
        breadcrumb: true,
        auth: true,
      },
      component: Layout,
      children: [
        {
          path: 'users',
          meta: {
            title: '用户管理',
            icon: 'User',
            breadcrumb: true,
            auth: true,
          },
          name: 'usersIndex',
          component: () => import('./../views/admin/users/index.vue')
        },
        {
          path: 'roles',
          meta: {
            title: '角色管理',
            icon: 'Finished',
            breadcrumb: true,
            auth: true,
          },
          name: 'rolesIndex',
          component: () => import('./../views/admin/roles/index.vue')
        }
      ]
    },

    // 待处理清单
    {
      path: '/pending',
      name: 'pending',
      meta: {
        title: '待处理清单',
        icon: 'Notebook',
        isMenu: false,
        hidden: true,
        breadcrumb: true,
      },
      component: Layout,
      redirect: '/pending/index',
      children: [
        {
          path: 'index',
          meta: {
            title: '待处理清单',
            hidden: true,
          },
          component: () => import('../../src/views/pending/index.vue')
        }
      ]
    },



    // 查找挂号学生信息
    {
      path: '/registers',
      name: 'registers',
      meta: {
        title: '开始挂号',
        icon: 'FirstAidKit',
        isMenu: true,
        breadcrumb: true,
        auth: true,
      },
      component: Layout,
      redirect: '/registers/index',
      children: [
        {
          path: 'index',
          meta: {
            title: '挂号',
            hidden: true,
            auth: true,
          },
          component: () => import('../../src/views/registers/index.vue')
        },
        {
          path: 'fill',
          name: 'registerFill',
          meta: {
            title: '填充挂号信息',
            hidden: true,
            auth: true,
          },
          component: () => import('../../src/views/registers/fillinfo.vue')
        }
      ]
    },

    // 就诊记录
    {
      path: '/records',
      name: 'records',
      meta: {
        title: '就诊记录',
        icon: 'MessageBox',
        isMenu: true,
        breadcrumb: true,
        auth: true,
      },
      component: Layout,
      redirect: '/records/index',
      children: [
        {
          path: 'index',
          meta: {
            title: '就诊记录',
            hidden: true,
            breadcrumb: true,
            auth: true,
          },
          component: () => import('../../src/views/records/index.vue')
        },
        {
          path: 'edit',
          name: 'editRecord',
          meta: {
            title: '编辑就诊记录',
            hidden: true,
            auth: true,
          },
          component: () => import('@/views/records/editRecord.vue')
        }
      ]
    },

    // 报表输出
    {
      path: '/export',
      name: 'export',
      meta: {
        title: '报表输出',
        icon: 'Printer',
        isMenu: true,
        breadcrumb: true,
        auth: true,
      },
      component: Layout,
      redirect: '/export/index',
      children: [
        {
          path: 'index',
          meta: {
            title: '报表输出',
            auth: true,
          },
          component: () => import('../../src/views/export/index.vue')
        }
      ]
    },

    // 报表输出
    {
      path: '/condition',
      name: 'condition',
      meta: {
        title: '健康状态',
        icon: 'Reading',
        isMenu: true,
        breadcrumb: true,
        auth: true,
      },
      component: Layout,
      redirect: '/condition/index',
      children: [
        {
          path: 'index',
          meta: {
            title: '健康状态',
            auth: true,
          },
          component: () => import('@/views/options/condition/index.vue'),
        }
      ]
    },
    
    // 系统配置
    {
      path: '/options',
      name: 'options',
      meta: {
        title: '系统配置',
        icon: 'Setting',
        isMenu: true,
        breadcrumb: true,
        auth: true,
      },
      component: Layout,
      children: [
        {
          path: 'users',
          meta: {
            title: '用户管理',
            icon: 'User',
            breadcrumb: true,
            auth: true,
          },
          name: 'usersIndex',
          component: () => import('./../views/admin/users/index.vue')
        },
        {
          path: 'places',
          meta: {
            title: '事发地点',
            icon: 'Location',
            breadcrumb: true,
            auth: true,
            
          },
          component: () => import('../../src/views/options/places/index.vue')
        },
        {
          path: 'causes',
          meta: {
            title: '意外伤害原因',
            icon: 'DataLine',
            breadcrumb: true,
            auth: true,
          },
          component: () => import('../../src/views/options/causes/index.vue')
        },
        {
          path: 'ways',
          meta: {
            title: '处理方式',
            icon: 'Guide',
            breadcrumb: true,
            auth: true,
          },
          component: () => import('@/views/options/ways/index.vue')
        },
        {
          path: 'groups',
          meta: {
            title: '就诊原因',
            icon: 'Notebook',
            breadcrumb: true,
            auth: true,
          },
          component: () => import('@/views/options/reasons/index.vue')
        },
        
      ]
    },
    
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  if(to.meta && to.meta.title) {
      document.title = to.meta.title + ' - 校园医务挂号系统'
  }

  const tokenObj = JSON.parse(sessionStorage.getItem('kcisxa-healthcenter-token'));
  if(tokenObj && tokenObj.token) {
      next()
  } else { // 未登录
    if(to.meta.auth) {
        ElMessage({message: '用户未登录', type: 'warning'})
        router.push({name:'login'}) 
    } else {
        next() 
    }
  }

})

export default router